package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.UserGroup;
import com.fahai.web.service.SysGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by sky on 17/5/11.
 * 公司部门管理的类,隶属于账号管理模块
 */
@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping("/sysgroup")
public class SysGroupController extends BaseController {
    static Logger logger = LoggerFactory.getLogger(SysGroupController.class);

    @Autowired
    SysGroupService sysGroupService;

    /**
     * 查询部门
     *
     * @param pageVo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserGroup", method = RequestMethod.GET)
    public JsonResult getUserGroup(PageVo pageVo){
        pageVo = sysGroupService.findUserGroupList(pageVo);
        return ok("查询成功",pageVo);
    }

    /**
     * 修改公司部门名称
     *
     * @param userGroupId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSysGroup", method = RequestMethod.GET)
    public JsonResult updateUserGroupName(@RequestParam int userGroupId, @RequestParam String groupName) {
        UserGroup userGroup = new UserGroup();
        userGroup.setUserGroupId(userGroupId);
        userGroup.setGroupName(groupName);
        sysGroupService.updateUserGroup(userGroup);
        return ok("修改部门成功");
    }

    /**
     * 增加公司部门名字
     *
     * @param groupName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveSysGroup", method = RequestMethod.GET)
    public JsonResult saveUserGroup(@RequestParam String groupName) {
        UserGroup userGroup = new UserGroup();
        userGroup.setGroupName(groupName);
        sysGroupService.saveUserGroup(userGroup);
        return ok("增加部门成功");
    }

    /**
     * 删除部门名称, 暂无此需求
     * 删除部门时要考虑是否有关联的用户
     *
     * @param userGroupId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSysGroup", method = RequestMethod.GET)
    public JsonResult deleteUserGroup(@RequestParam Integer userGroupId) {
        UserGroup userGroup = new UserGroup();
        userGroup.setUserGroupId(userGroupId);
        sysGroupService.deleteUserGroup(userGroup);
        return ok("删除部门成功");
    }

}
